import store from '@/store';
const getConstData = async (code) => {
	return await store.dispatch('getConstByCode', code);
}
const getEnumData = async (code) => {
	return await store.dispatch('getEnumByCode', code);
}
export const dictMixins = {
	data() {
		return {
			dictData: {},
			screenWidth: 0
		}
	},
	computed: {
		isMobile() {
			return this.screenWidth < 768;
		},
		isPad() {
			return this.screenWidth < 992 && this.screenWidth >= 768;
		}
	},
	created() {
		this.screenWidth = window.innerWidth;
		window.addEventListener('resize', () => {
			this.screenWidth = window.innerWidth;
		});
		if (!!this.$options.consts) {
			this.getDictDatas(this.$options.consts, 'const');
		}
		if (!!this.$options.enums) {
			this.getDictDatas(this.$options.enums, 'enum');
		}
	},
	methods: {
		async getDictDatas(arr, type) {
			let promiseArr = arr.map(code => {
				if (type === 'const') {
					return !!code && getConstData(code);
				} else if (type === 'enum') {
					return !!code && getEnumData(code);
				}
			});
			let dictArr = await Promise.all(promiseArr);
			dictArr.forEach(item => {
				this.$set(this.dictData, item.code, item.value);
			});
		}
	}
}